package com.zjy.business.system.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjy.business.system.dao.SysOperationLogDao;
import com.zjy.business.system.dao.SysUserDao;
import com.zjy.business.system.domain.entity.SysUser;
import com.zjy.business.system.domain.manager.query.ManagerListPageSysOperationQuery;
import com.zjy.business.system.domain.manager.vo.ManagerListPageSysOperationVo;
import com.zjy.business.system.domain.manager.vo.ManagerSysOperationTypeVo;
import com.zjy.business.system.manager.service.ManagerSysOperationService;
import com.zjy.common.constant.StatusEnums;
import com.zjy.log.enums.LogBusinessType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author yxb
 * create_time: 2024-10-24 10:05
 * description: 
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ManagerSysOperationServiceImpl implements ManagerSysOperationService {

    final private SysOperationLogDao sysOperationLogDao;

    final private SysUserDao sysUserDao;

    @Override
    public PageInfo<ManagerListPageSysOperationVo> listPageSysOperation(ManagerListPageSysOperationQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<ManagerListPageSysOperationVo> voList = sysOperationLogDao.listPageSysOperation(query);
        Map<Long, SysUser> sysUserMap = new HashMap<>();
        voList.forEach(item -> {
            if (Objects.equals(item.getOperatorType(), StatusEnums.LOGIN_USER_TYPE_ADMIN.getStatus())) {
                item.setSysUser(sysUserDao.findCacheOneByMap(SysUser::getId, item.getOperationUser(), sysUserMap));
            }
            item.setBusinessTypeName(LogBusinessType.of(item.getBusinessType()));
        });
        return new PageInfo<>(voList);
    }

    @Override
    public List<ManagerSysOperationTypeVo> typeDownList() {
        List<ManagerSysOperationTypeVo> result = new ArrayList<>();
        for (LogBusinessType value : LogBusinessType.values()) {
            ManagerSysOperationTypeVo vo = new ManagerSysOperationTypeVo();
            vo.setType(value.getType());
            vo.setBusinessTypeName(value.getMsg());
            result.add(vo);
        }
        return result;
    }
}
